<template>
  <div class="chat">
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="小智同学"
    ></van-nav-bar>
    <!-- 聊天列表 -->
    <div class="chat-list" ref="list">
      <div v-for="(item, index) in list" :key="index">
        <div class="chat-item left" v-if="item.name === 'xz'">
          <geek-icon name="xiaozhi" />
          <div class="chat-pao">{{ item.msg }}</div>
        </div>
        <div class="chat-item right" v-else>
          <div class="chat-pao">{{ item.msg }}</div>
          <van-image
            fit="cover"
            round
            :src="$store.state.user.userProfile.photo"
          />
        </div>
      </div>
    </div>
    <!-- 输入框 -->
    <div class="reply-container van-hairline--top">
      <van-field
        v-model.trim="value"
        left-icon="edit"
        placeholder="请描述您的问题"
      ></van-field>
      <span class="send" @click="send">发送</span>
    </div>
  </div>
</template>

<script>
import socketIo from 'socket.io-client'
export default {
  name: 'Chat',
  data () {
    return {
      list: [],
      value: '',
      io: ''
    }
  },
  methods: {
    // 用户发送消息
    send () {
      if (this.value !== '') {
        this.io.emit('message', {
          msg: this.value,
          timestamp: Date.now()
        })
        this.list.push({
          name: 'my',
          msg: this.value
        })
        this.value = ''
        this.$nextTick(() => {
          this.$refs.list.scrollTop = 99999
        })
      }
    }
  },
  created () {
    this.$store.dispatch('user/getUserProfile')
    // ws 连接接口地址
    this.io = socketIo(process.env.VUE_APP_URL, {
      query: {
        token: this.$store.state.user.token
      },
      transports: ['websocket']
    })
    // 监听是否连接成功
    this.io.on('connect', () => {
      this.list.push({
        name: 'xz',
        msg: '我是小智同学，需要什么帮助吗？'
      })
    })
    // 监听服务器返回的消息
    this.io.on('message', res => {
      this.list.push({
        name: 'xz',
        msg: res.msg
      })
      this.$nextTick(() => {
        this.$refs.list.scrollTop = 99999
      })
    })
  },
  beforeDestroy () {
    // 退出页面时断开连接
    this.io.close()
  }
}
</script>

<style lang="less" scoped>
.chat {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background: #fff;
  padding: 46px 0 50px 0;

  .chat-list {
    height: 100%;
    overflow-y: scroll;

    .chat-item {
      padding: 10px;

      .van-image {
        vertical-align: middle;
        width: 40px;
        height: 40px;
      }

      .geek-icon {
        font-size: 40px;
        line-height: 0;
      }

      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 20px;
        border-radius: 4px;
        position: relative;
        padding: 10px;
        background-color: @geek-gray-color;
        word-break: break-all;
        font-size: 14px;
        color: #333;

        &::before {
          content: '';
          width: 6px;
          height: 6px;
          position: absolute;
          top: 15px;
          background: @geek-gray-color;
        }
      }
    }
  }
}

.chat-item.right {
  text-align: right;

  .chat-pao {
    margin-left: 0;
    margin-right: 10px;

    &::before {
      right: -3px;
      transform: rotate(45deg);
    }
  }
}

.chat-item.left {
  text-align: left;

  .chat-pao {
    margin-left: 10px;
    margin-right: 0;

    &::before {
      left: -3px;
      transform: rotate(-135deg);
    }
  }
}

.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 49px;
  width: 100%;
  background: #fff;
  z-index: 9999;
  display: flex;
  align-items: center;
  padding: 0 10px;

  > .van-field {
    flex: 1;
    background: #f7f8fa;
    height: 32px;
    border-radius: 16px;
    padding: 0 10px;
    line-height: 32px;

    ::v-deep .van-field__left-icon .van-icon {
      color: #ccc;
    }
  }

  > .send {
    margin-left: 10px;
    font-size: 14px;
    color: #999;
  }
}
</style>
